import 'package:flutter/material.dart';
import 'package:love_app/common/su.dart';
import 'package:love_app/enum/sex.dart';
import 'package:love_app/providers/attention_provider.dart';
import 'package:love_app/providers/details_data_provider.dart';
import 'package:love_app/providers/latest_provider.dart';
import 'package:love_app/providers/recommend_provider.dart';
import 'package:love_app/providers/share_provider.dart';
import 'package:love_app/providers/user_center_provider.dart';
import 'package:love_app/service/comment_details_api.dart';
import 'package:provider/provider.dart';
import 'package:love_app/model/comment_detailst_model.dart' as CDModel;

// 回复评论框
class ShowCommunityInput extends StatefulWidget {
  final int itemId;
  final int rId;
  final String providerType;
  final int firstId;
  final CDModel.To_user toUser;

  const ShowCommunityInput({Key key, @required this.itemId,@required this.rId,@required this.providerType, this.firstId, this.toUser})
      : super(key: key);

  @override
  _ShowCommunityInputState createState() => _ShowCommunityInputState();
}

class _ShowCommunityInputState extends State<ShowCommunityInput> {
  ShareProvider _shareProvider; // 全局共享数据
  DeatilsDataProvider _deatilsDataProvider; // 社区页面的文章评论共享数据
  dynamic _provider; // 社区页面的文章评论共享数据
  TextEditingController _commentController; // input控制器
  FocusNode _commentFocusNode1; // 输入法焦点

  @override
  void initState() {
    super.initState();
    _commentController = TextEditingController();
    _commentFocusNode1 = FocusNode();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _shareProvider = Provider.of<ShareProvider>(context, listen: false);
    _deatilsDataProvider =
        Provider.of<DeatilsDataProvider>(context, listen: false);
    if (widget.providerType == 'recommend') {
      _provider = Provider.of<RecommendProvider>(context, listen: false);
    } else if (widget.providerType == 'latest') {
      _provider = Provider.of<LatestProvider>(context, listen: false);
    } else if (widget.providerType == 'attention') {
      _provider = Provider.of<AttentionProvider>(context, listen: false);
    } else if (widget.providerType == 'userCenter'){
      _provider = Provider.of<UserCenterProvider>(context, listen: false);
    }
  }

  @override
  void dispose() {
    super.dispose();
    _commentController.dispose();
    _commentFocusNode1.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: _textInputAndIcon(widget.rId),
    );
  }

  // 回复评论框
  Widget _textInputAndIcon(int rId) {
    return Scrollbar(
      // 使焦点取消
      child: GestureDetector(
        onTap: () {
          Navigator.pop(context);
        },
        child: SingleChildScrollView(
          child: Row(
            children: <Widget>[
              _textInput(),
              _inputIcon(rId),
            ],
          ),
        ),
      ),
    );
  }

  // 输入框
  Widget _textInput() {
    return Expanded(
      child: TextField(
        autofocus: false,
        focusNode: _commentFocusNode1,
        controller: _commentController,
        keyboardType: TextInputType.text,
        cursorColor: _shareProvider.textColor,
        // maxLines: 2,
        decoration: InputDecoration(
          hintText: "要不要留点什么。",
          border: null,
          contentPadding: EdgeInsets.only(
            left: Su().w(30),
            bottom: Su().h(5),
          ),
          focusedBorder: UnderlineInputBorder(
            //输入框被选中时的边框样式
            borderSide: BorderSide(color: _shareProvider.textColor),
          ),
        ),
      ),
    );
  }

  // 输入框右边的icon
  Widget _inputIcon(int rId) {
    return IconButton(
      icon: Icon(Icons.send),
      color: _shareProvider.textColor,
      onPressed: () async {
        String token = _shareProvider.token;
        String content = _commentController.text;
        int id = await CommentDetailsApi()
            .replyCommentsById(token, content, widget.itemId, rId);
        // _deatilsDataProvider.deatilsData.clear();
        _provider.updateCollectionsData(widget.itemId,);
        // 处理本地共享数据provider更新
        if(widget.firstId != -1){
          int index = _deatilsDataProvider.deatilsData.indexWhere((item)=> item.cId == widget.firstId);
          CDModel.User user = CDModel.User(_shareProvider.uid, _shareProvider.nickname, _shareProvider.headerImage, _shareProvider.sex == Sex.boy ? 1 : 2,);
          CDModel.Replies data = CDModel.Replies(id, user, widget.toUser, content, (DateTime.now().millisecondsSinceEpoch) ~/ 1000,);
          _deatilsDataProvider.deatilsData[index].replies.add(data);
          setState(() {});
        }
        Navigator.of(context).pop();
      },
    );
  }
}
